import { _decorator, Component, Label, Node, Sprite } from 'cc';
import { AudioMgr, SoundName } from '../Mgr/AudioMgr';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
import { ResMgr } from '../Mgr/ResMgr';
import { UIMgr } from '../Mgr/UIMgr';
import { AssetList } from '../Global';
import { TopPrompt } from '../Top/TopPrompt';
const { ccclass, property } = _decorator;

@ccclass('SingleShopCat')
export class SingleShopCat extends Component {

    //猫咪
    @property(Sprite)
    public Cat: Sprite = null;

    //金币数量
    @property(Label)
    public GoldLabel: Label = null;

    //猫咪索引
    public CatIndex: number = 0;

    /**
     * 更新
     */
    public UpdateCat(_index: number, _coin: number, _ishave: boolean) {
        this.CatIndex = _index;
        this.GoldLabel.string = _coin.toString();
        this.Cat.spriteFrame = null;

        if (_ishave) {
            this.GoldLabel.string = "0";
            this.Cat.spriteFrame = ResMgr.Instance.SpriteFrames["game_cat" + this.CatIndex];
        }
    }

    /**
     * 点击我
     */
    public OnClickMe() {
        AudioMgr.instance.PlayEffect(SoundName.Click);

        if (LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CATINFO[this.CatIndex].ishave) {
            let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
            ss.getComponent(TopPrompt).UpdateContent("已经拥有");
            return;
        }
        if (LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.COIN < LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CATINFO[this.CatIndex].coin) {
            let ss: Node = UIMgr.Instance.ShowTop(AssetList.Top.Prompt);
            ss.getComponent(TopPrompt).UpdateContent("金币不足");
            return;
        }


        LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.COIN -= LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CATINFO[this.CatIndex].coin;
        LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CATINFO[this.CatIndex].ishave = true;
        this.Cat.spriteFrame = ResMgr.Instance.SpriteFrames["game_cat" + this.CatIndex];
        LocalStorageMgr.Instance.UpdateLocalStroage();
    }

}


